﻿
using DynamicViewFormApp.Component;
using DynamicViewFormApp.Component.Handler;
using DynamicViewFormApp.Enum;
using DynamicViewFormApp.Service;
using DynamicViewFormApp.Setting;
using DynamicViewFormApp.SubForm;
using DynamicViewFormApp.Util;
using DynamicViewFormLibrary;
using DynamicViewFormLibrary.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace DynamicViewFormApp
{
    public partial class MainForm : Form
    {
        //List<Panel> pagePanelList = new List<Panel>();//分页集合
        StartForm startForm = null;

        public MainForm()
        {
            //初始化基础控件
            InitializeComponent();
        }
        private void MainForm_Load(object sender, EventArgs e)
        {
            //初始化主窗口
            this.WindowState = FormWindowState.Maximized;//窗体一启动就最大化

            //初始化菜单栏
            //toolStripMain.Enabled = false;
            //btnSaveWorkSpace.Enabled = false;
            flowLayoutPanelPageView.Enabled = true;

            this.toolStripComboBoxFontFamily.SelectedIndex = 0;
            this.toolStripComboBoxFontSize.SelectedIndex = 4;


            //绑定初始化控件事件
            this.pageAddButton.Click += new EventHandler(this.BtnCreateForm_Click);
            this.NewToolStripMenuItem.Click += new System.EventHandler(this.BtnCreateForm_Click);
            //this.NewToolStripButton.Click += new System.EventHandler(this.OpenNewForm_Click);

            //字体样式菜单栏事件绑定
            this.toolStripButton_Bold.Click += new EventHandler(ToolStripHandler.FontStyleBold_Click);//字体加粗
            this.toolStripButton_Italic.Click += new EventHandler(ToolStripHandler.FontStyleItalic_Click);//字体倾斜
            this.toolStripButton_Underline.Click += new EventHandler(ToolStripHandler.FontStyleUnderline_Click);
            this.toolStripButton_FontUp.Click += new EventHandler(ToolStripHandler.FontSizeUp_Click);
            this.toolStripButton_FontDown.Click += new EventHandler(ToolStripHandler.FontSizeDown_Click);

            this.toolStripButton_Content.Click += new EventHandler(ToolStripHandler.FontSizeContent_Click);
            this.toolStripButton_Header1.Click += new EventHandler(ToolStripHandler.FontSizeHeader1_Click);
            this.toolStripButton_Header2.Click += new EventHandler(ToolStripHandler.FontSizeHeader2_Click);
            this.toolStripButton_Header3.Click += new EventHandler(ToolStripHandler.FontSizeHeader3_Click);
            this.toolStripButton_Header4.Click += new EventHandler(ToolStripHandler.FontSizeHeader4_Click);
            this.toolStripButton_Header5.Click += new EventHandler(ToolStripHandler.FontSizeHeader5_Click);
            this.toolStripButton_Header6.Click += new EventHandler(ToolStripHandler.FontSizeHeader6_Click);

            //滚动居中
            this.panelBase.Resize += new System.EventHandler(PanelBaseHandler.PanelBase_Resize);

            //新建工作区
            this.toolStripButton2.Click += new EventHandler(this.BtnCreateForm_Click);
            this.NewToolStripButton.Click += new EventHandler(this.BtnCreateForm_Click);

            //打开新建窗口
            //FormBaseService.OpenNewForm(this, "欢迎使用");
            FormBaseService.OpenStartForm(this);
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        public void InitData()
        {

            //toolStripMain.Enabled = true;
            //btnSaveWorkSpace.Enabled = true;
            flowLayoutPanelPageView.Enabled = true;
            btnExportImg.Enabled = true;
            btnExportDoc.Enabled = true;

            Panel page = PageService.CreateNewPage(panelBase);
            this.CreatePage(page);

            //初始化控件组
            PaperSetting.CurrentPageId = page.Name;
            if (PublicVariable.IsCanNew)
            {
                this.pageAddButton.Click -= new EventHandler(this.BtnCreateForm_Click);
                this.pageAddButton.Click -= new EventHandler(this.CreateNewPage_Click);
                this.toolStripButton2.Click -= new System.EventHandler(this.BtnCreateWorkSpace_Click);
                this.NewToolStripButton.Click -= new System.EventHandler(this.BtnCreateWorkSpace_Click);

                this.pageAddButton.Click -= new EventHandler(this.BtnCreateForm_Click);
                this.pageAddButton.Click += new EventHandler(this.CreateNewPage_Click);

                this.toolStripButton2.Click += new System.EventHandler(this.BtnCreateWorkSpace_Click);
                this.NewToolStripButton.Click += new System.EventHandler(this.BtnCreateWorkSpace_Click);

                this.toolStripButton2.Click -= new EventHandler(this.BtnCreateForm_Click);
                this.NewToolStripButton.Click -= new EventHandler(this.BtnCreateForm_Click);
            }

            this.CreateExamBasicElement();

            switch (PaperSetting.Type)
            {
                case "A3":
                    this.CreatePage();
                    break;
                case "A4":
                    this.CreatePage();
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 插入剪贴板内容
        /// </summary>
        public void PasteContent()
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.Paste();
            }
            else
            {
                MessageBox.Show("当前内容已复制到剪贴板中");
            }
        }
        public void ShowNewForm(StartForm startForm)
        {
            this.startForm = startForm;
            //打开新建窗口
            FormBaseService.OpenCreateForm(this, "新建答题卡");
        }
        public void CloseStartForm()
        {

            if (this.startForm != null)
            {
                this.startForm.Close();
            }
        }
        /// <summary>
        /// 新增分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateNewPage_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否添加分页？", "提示：", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.CreatePage();
            }

        }

        /// <summary>
        /// 创建新分页
        /// </summary>
        private void CreatePage()
        {
            Panel page = PageService.CreateNewPage(panelBase);
            this.CreatePage(page);
            RichTextBoxService.CreateBlockContentArea(page);//生成题块区域
        }

        /// <summary>
        /// 创建新的分页
        /// </summary>
        /// <param name="page"></param>
        private void CreatePage(Panel page)
        {
            List<Panel> pagePanelList = PageSetting.PagePanelList;
            page.Tag = pagePanelList.Count;
            pagePanelList.Add(page);

            Button pageButton = ButtonService.CreatePageButton(pagePanelList.Count.ToString(), page.Name);

            pageButton.Click += new EventHandler(this.PageIndexButton_Click);

            flowLayoutPanelPageView.Controls.Add(pageButton);

            PageSetting.PageSize = pagePanelList.Count;

            ToolStripStatusService.PageInfoToolStripLabel(toolStripStatusPageSizeLabel);
        }

        /// <summary>
        /// 页面滚动条事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PageIndexButton_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            PageSetting.PageIndex = btn.Text;
            string ctlId = btn.Tag.ToString();//控件ID
            PaperSetting.CurrentPageId = ctlId;
            panelBase.AutoScrollPosition = new Point(0, 0);//重置滚动条，消除偏移量
            Panel panel = (Panel)ControlFindUtil.GetControlByName(panelBase, ctlId);
            if (panel != null)
            {
                panelBase.AutoScrollPosition = new Point(0, panel.Location.Y - A4.Span);
            }
            ToolStripStatusService.PageInfoToolStripLabel(toolStripStatusPageSizeLabel);
        }

        /// <summary>
        /// 创建考试基础元素
        /// </summary>
        private void CreateExamBasicElement()
        {
            Control page = ControlFindUtil.GetControlByName(panelBase, PaperSetting.CurrentPageId);
            if (page != null)
            {
                //生成考试标题
                RichTextBox pre = RichTextBoxService.CreateTestTitleElement(page);
                RichTextBoxService.CreateTestExamineeInfo(page, pre.Top, pre.Left, pre.Width, pre.Height);
                RichTextBoxService.CreateFullFillingRecognition(page, pre.Top, pre.Left, pre.Width, pre.Height);
                List<RichTextBox> rtbs = RichTextBoxService.CreateBlockContentArea(page);
                if (rtbs != null && rtbs.Count > 0)
                {
                    PublicVariable.CurrentRichTextBox = rtbs[0];
                    RichTextBox rtb = rtbs[0];
                    rtb.Focus();
                    rtb.ScrollToCaret();
                }

                pre.TextChanged += new EventHandler(FormText_Changed);
            }
        }

        /// <summary>
        /// 窗口标题事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormText_Changed(object sender, EventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;
            this.Text = rtb.Text;
        }



        /// <summary>
        /// 打开新建答题卡窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCreateForm_Click(object sender, EventArgs e)
        {
            FormBaseService.OpenCreateForm(this, "新建答题卡");
        }
        #region 工具栏事件
        /// <summary>
        /// 工具栏——打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripButton_Open_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "*.RTF|*.rtf";
                dlg.CheckFileExists = true;
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    RichTextBox rtb = new RichTextBox();
                    rtb.LoadFile(dlg.FileName);
                    Clipboard.SetData(DataFormats.Rtf, rtb.Rtf);
                    //LoadFile(dlg.FileName);
                    PublicVariable.CurrentRichTextBox.Paste();
                }
            }
        }
        /// <summary>
        /// 客观题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripButton_ObjectiveItem_Click(object sender, EventArgs e)
        {
            Form form = new ObjectiveItemForm();
            form.StartPosition = FormStartPosition.CenterScreen;
            form.ShowDialog(this);
        }

        private void ToolStripButton_WritingChinese_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Rtf, RtfContent.WritingChinese);
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.Paste();
            }
        }

        private void ToolStripButton_WritingEnglish_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Rtf, RtfContent.WritingEnglish);
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.Paste();
            }
        }

        private void ToolStripButton_BlockCentre_Click(object sender, EventArgs e)
        {
            Form form = new BlockCentreForm();
            form.StartPosition = FormStartPosition.CenterScreen;
            form.ShowDialog(this);
        }

        private void ToolStripButton_GapFill_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
            Clipboard.SetData(DataFormats.Text, RtfContent.GapFillItem);
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.Paste();
            }
        }

        private void ToolStripButton_Copy_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.Copy();
            }
        }

        private void ToolStripButton_Cut_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.Cut();
            }
        }

        private void ToolStripButton_Paste_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.Paste();
            }
        }

        private void ToolStripButton_FontLeft_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.SelectionAlignment = HorizontalAlignment.Left;
                PublicVariable.CurrentRichTextBox.SelectionIndent = 0;
            }
        }

        private void ToolStripButton_FontCenter_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.SelectionAlignment = HorizontalAlignment.Center;
                PublicVariable.CurrentRichTextBox.SelectionIndent = 0;
            }
        }

        private void ToolStripButton_FontRight_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.SelectionAlignment = HorizontalAlignment.Right;
                PublicVariable.CurrentRichTextBox.SelectionIndent = 0;
            }
        }

        private void ToolStripButton_Indent_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                int indent = PublicVariable.CurrentRichTextBox.SelectionIndent;
                Font font = PublicVariable.CurrentRichTextBox.SelectionFont;
                if (font != null)
                {
                    int fontSize = (int)font.Size;
                    if (indent >= 0)
                    {
                        PublicVariable.CurrentRichTextBox.SelectionIndent = indent + fontSize;
                    }
                }
            }
        }

        private void ToolStripButton_Outdent_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                int indent = PublicVariable.CurrentRichTextBox.SelectionIndent;
                Font font = PublicVariable.CurrentRichTextBox.SelectionFont;
                if (font != null)
                {
                    int fontSize = (int)font.Size;

                    if (indent >= fontSize)
                    {
                        PublicVariable.CurrentRichTextBox.SelectionIndent = indent - fontSize;
                    }
                    else
                    {
                        PublicVariable.CurrentRichTextBox.SelectionIndent = 0;
                    }
                }
            }
        }

        private void ToolStripComboBoxFontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ToolStripComboBox tscb = sender as ToolStripComboBox;
            float.TryParse(tscb.SelectedItem.ToString(), out float fnum);
            if (PublicVariable.CurrentRichTextBox != null)
            {
                // Ensure that text is currently selected in the text box.   
                if (PublicVariable.CurrentRichTextBox.SelectedText.Length > 0)
                {
                    Font font = PublicVariable.CurrentRichTextBox.SelectionFont;
                    if (font != null)
                    {
                        PublicVariable.CurrentRichTextBox.SelectionFont = new Font(font.FontFamily, fnum);
                    }
                }
            }
        }

        private void ToolStripButton_Help_Click(object sender, EventArgs e)
        {

        }

        private void ToolStripButton_Print_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 导出图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripButton_Export_Imgage_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                PublicVariable.CurrentRichTextBox.SelectionLength = 0;

                folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
                folderBrowserDialog.Description = "请选择文件夹";
                DialogResult d = folderBrowserDialog.ShowDialog();
                if (d == DialogResult.OK)
                {
                    string path = folderBrowserDialog.SelectedPath;

                    PageSetting.PagePanelList.ToString();
                    foreach (Panel p in PageSetting.PagePanelList)
                    {
                        if (p != null)
                        {
                            Bitmap b = new Bitmap(p.Width, p.Height);
                            p.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height));
                            b.Save(@path + "\\" + p.Name + ".png", ImageFormat.Png);
                        }
                    }

                    MessageBox.Show("图片已导出！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        #endregion

        private void ToolStripMenuItem_Exit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确定退出?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                Environment.Exit(0);
            }
        }

        private void SaveToolStripButton_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {

                using (SaveFileDialog dlg = new SaveFileDialog())
                {
                    dlg.Title = "请选择保存文件路径";
                    dlg.Filter = "RTF文档(*.rtf)|*.rtf";
                    dlg.OverwritePrompt = true;  //是否覆盖当前文件
                    dlg.RestoreDirectory = true;  //还原目录
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        string fileName = dlg.FileName;
                        string rtfText = PublicVariable.CurrentRichTextBox.Rtf;
                        byte[] bytes = Encoding.Default.GetBytes(rtfText);
                        FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
                        fs.Write(bytes, 0, bytes.Length);
                        fs.Flush();
                        fs.Close();
                    }
                }
            }

        }

        /// <summary>
        /// 导出文档文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnExportDoc_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {

                using (SaveFileDialog dlg = new SaveFileDialog())
                {
                    dlg.Title = "请选择保存文件路径";
                    dlg.Filter = "RTF文档(*.rtf)|*.rtf";
                    dlg.OverwritePrompt = true;  //是否覆盖当前文件
                    dlg.RestoreDirectory = true;  //还原目录
                    dlg.FileName = ExamInfo.ExamName;
                    dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//获取桌面目录
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {

                        RtfDomDocuments doc = new RtfDomDocuments();

                        string paperSize = PaperSetting.Type;
                        switch (paperSize)
                        {
                            case "A4":
                                doc.LoadTemplate(RtfContent.TemplateA4);
                                break;
                            case "A3":
                                doc.LoadTemplate(RtfContent.TemplateA3);
                                break;
                        }
                        doc.FormatTemplate(ExamInfo.ExamName);

                        List<RichTextBox> rtbs = PageSetting.RichTextBoxeList;

                        foreach (RichTextBox rtb in rtbs)
                        {
                            rtb.SelectAll();
                            rtb.SelectionFont = FontSetting.FontFormat;
                            rtb.DeselectAll();
                            string rtfText = rtb.Rtf;
                            doc.Append(rtb.Rtf);
                        }

                        doc.Save(dlg.FileName);

                        MessageBox.Show("文件已导出！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }


        /// <summary>
        /// 插入题块边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnImportBorder_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {
                Clipboard.Clear();
                Clipboard.SetData(DataFormats.Rtf, RtfContent.BorderBox);
                PublicVariable.CurrentRichTextBox.Paste();
            }
        }
        /// <summary>
        /// 插入图片对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnImportImage_Click(object sender, EventArgs e)
        {
            if (PublicVariable.CurrentRichTextBox != null)
            {

                using (OpenFileDialog dlg = new OpenFileDialog())
                {
                    dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff";
                    dlg.Title = "打开图片";
                    dlg.CheckFileExists = true;
                    if (dlg.ShowDialog(this) == DialogResult.OK)
                    {
                        try
                        {
                            Bitmap bmp = new Bitmap(dlg.FileName);//获得图片
                            Clipboard.SetData(DataFormats.Bitmap, bmp);
                            if (PublicVariable.CurrentRichTextBox.CanPaste(DataFormats.GetFormat(DataFormats.Bitmap)))
                            {
                                PublicVariable.CurrentRichTextBox.Paste();
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("请选择正确的图片");
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 主窗口关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormApplicationClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dialogClose = MessageBox.Show("是否确认关闭窗口？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dialogClose != DialogResult.OK)
            {
                e.Cancel = true;
            }
        }
        #region 工作区管理
        /// <summary>
        /// 新建工作区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCreateWorkSpace_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否新建答题卡?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                CreateApplicationInstance();
            }
        }

        /// <summary>
        /// 创建应用程序实例
        /// </summary>
        private void CreateApplicationInstance()
        {
            Thread thread = new Thread(new ParameterizedThreadStart(RunApplicationProcess));
            object appName = Application.ExecutablePath;
            thread.Start(appName);
        }
        /// <summary>
        /// 启动应用程序实例
        /// </summary>
        /// <param name="obj"></param>
        private void RunApplicationProcess(Object obj)
        {
            Process process = new Process();
            process.StartInfo.FileName = obj.ToString();
            process.Start();
        }
        /// <summary>
        /// 保存工作区文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSaveWorkSpace_Click(object sender, EventArgs e)
        {
            bool isSaveSuccess = SaveWorkSpace();
            if (isSaveSuccess)
            {
                MessageBox.Show("工作区已保存！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("当前保存操作无效！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// 保存工作区
        /// </summary>
        /// <returns></returns>
        private bool SaveWorkSpace()
        {
            WorkSpaceService workSpace = new WorkSpaceService();
            bool isSaveSuccess = workSpace.SaveActivity();
            return isSaveSuccess;
        }

        /// <summary>
        /// 加载工作区文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLoadWorkSpace_Click(object sender, EventArgs e)
        {
            try
            {
                using (OpenFileDialog dlg = new OpenFileDialog())
                {
                    dlg.Filter = "答题卡工作区|*.rtfs";
                    dlg.Title = "请选择文件";
                    dlg.CheckFileExists = true;
                    dlg.InitialDirectory = FilePathUtil.RTFWorkSpaceRootPath();

                    if (dlg.ShowDialog(this) == DialogResult.OK)
                    {
                        string fileName = dlg.FileName;
                        string name = Path.GetFileNameWithoutExtension(dlg.FileName);//文件名没有扩展名
                        string paperName = name.Split('_')[0];
                        string paperType = name.Split('_')[1];
                        LoadWorkSpaceFile(paperName, paperType, fileName);

                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("文件无效");
            }
        }
        #endregion

        /// <summary>
        /// 加载工作控件文件
        /// </summary>
        public void LoadWorkSpaceFile(string paperName, string paperType, string fileName)
        {
            LoadingHelper.ShowLoadingScreen();//显示
            //清空panel中所有控件
            this.panelBase.Controls.Clear();
            int ctrls = this.flowLayoutPanelPageView.Controls.Count;
            while (ctrls > 1)
            {
                ctrls -= 1;
                this.flowLayoutPanelPageView.Controls.RemoveAt(ctrls);
            }
            //初始化Setting参数
            PageSetting.Init();
            //设置基础参数
            ExamInfo.ExamName = paperName;
            PaperSetting.Type = paperType;
            PublicVariable.IsCanNew = true;
            //生成页面基础元素
            this.InitData();
            LoadingHelper.CloseForm();//关闭

            RtfDomDocuments doc = new RtfDomDocuments();
            List<StringBuilder> rtfData = doc.LoadFile(fileName);
            int rtfDataCount = rtfData.Count;
            int rtbCount = PageSetting.RichTextBoxeList.Count;
            if (rtfDataCount > rtbCount)
            {
                int difCount = rtfDataCount - rtbCount;
                int pageCount = difCount / 2;
                int remainderCount = difCount % 2;
                if (remainderCount != 0)
                {
                    pageCount += 1;
                }
                for (int i = 0; i < pageCount; i++)
                {
                    this.CreatePage();
                }
            }

            int index = 0;
            if (PageSetting.RichTextBoxeList != null)
                foreach (RichTextBox rtb in PageSetting.RichTextBoxeList)
                {
                    if (index <= rtfDataCount)
                    {
                        rtb.Rtf = rtfData[index].ToString();
                        index++;
                    }
                }

            MessageBox.Show("答题卡已加载！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void ToolStripLabel_ResetPaper_Click(object sender, EventArgs e)
        {
            string pageSize = "A4";
            if (PaperSetting.Type != null)
            {
                pageSize = PaperSetting.Type;
            }
            LoadWorkSpaceFile("考试标题", pageSize, "");
        }
    }
}
